<?php
class User extends CI_Controller{
	private $per_page_index = 1;
	
	public function __construct(){
		parent::__construct();
		if(!Auth::isLoggedIn() ) redirect("/");
		$this->load->model("model_user");
	}
	function index(){
		if( ($this->session->userdata("type_user") != "admin") && ($this->session->userdata("type_user") != "superadmin") ) redirect("home/denied");
		$data['title'] = TITLE.' - Manage User';
		$data['javascript'][] = "ajax_paging.js";
		$data['javascript'][] = "admin_user.js";
		
		$params['order_by'] = "username asc, user_id asc";
		
		$count = count($this->model_user->getUserList($params));
		
		$this->load->library('paging');
		$config['base_url']   = base_url().'admin/user/load';
		$config['uri_segment'] = 4;
		$config['total_rows'] = $count;
		$config['per_page']   = $this->per_page_index;
		
		$data['pagination'] = Paging::setPagination($config);
		
		$params['limit_1'] = $this->per_page_index;
		$params['limit_2'] = 0;
		$data['user_list'] = $this->model_user->getUserList($params);
		$data['per_page_index'] = $this->per_page_index;
		$data['embedString'] = "";
		$this->load->view('admin/user',$data);
	}
	function load($offset=0){
		$key = trim($this->input->get("q",true));
		$keyword_decode = rawurldecode($key);
		$embedString = '';
		if($key != ""){
			$embedString = '?q='.rawurlencode($key);
		}
		$params['keyword'] = $keyword_decode;
		
		$order_by = "username asc, user_id asc";
		
		$sort_by_field = $this->input->get("s",true);
		$sort_by_type = $this->input->get("t",true);
		
		if($this->input->get("s",true) ){
			$order_by = $sort_by_field . " " . $sort_by_type;
			$embedString = '?s='.$sort_by_field.'&t='.$sort_by_type;
		}
			
		
		$params['order_by'] = $order_by;
		$count = count($this->model_user->getUserList($params));
		
		$this->load->library('paging');
		$config['base_url']   = base_url().'admin/user/load';
		$config['uri_segment'] = 4;
		$config['total_rows'] = $count;
		$config['per_page']   = $this->per_page_index;
		
		$data['pagination'] = Paging::setPagination($config);
		//manipulate pagination link to embed the suffix needed ok
		$data['pagination'] = Paging::embedSuffixInPaging($embedString,$data['pagination']);
		
		
		$params['limit_1'] = $this->per_page_index;
		$params['limit_2'] = $offset;
		$data['user_list'] = $this->model_user->getUserList($params);
		$data['per_page_index'] = $this->per_page_index;
		$data['embedString'] = $embedString;
		$this->load->view('admin/content_load/user_load',$data);
	}
	
	function detail($user_id){
		$this->session->set_flashdata('detail_url','user');
		if( ($this->session->userdata("type_user") != "admin") && ($this->session->userdata("type_user") != "superadmin") ) redirect("/");
		$data['title'] = TITLE.' - User Detail';
		$this->load->view('admin/user_detail',$data);
	}
	function karyawan(){
		if( ($this->session->userdata("type_user") != "admin") && ($this->session->userdata("type_user") != "superadmin") ) redirect("home/denied");
		$data['user'] = $this->model_user->getKarList();
		$this->load->view("admin/karList",$data);
	}
	
	function edit(){
		$id =$this->uri->segment(4, 0);
		$data['title'] = TITLE.' - Ubah Data Karyawan';
		if($this->input->post("btnSave")){
			$hasil = $this->_saveDetail($id);
			if($hasil) $data['success'] = "Data Sudah Dirubah";
		}
		
		$detail = $this->model_user->getDetail($id);
		$data['detail'] = $detail;
		$this->load->view("admin/karEdit",$data);
	
	}
	function _saveDetail($id){
		$this->form_validation->set_error_delimiters('', '');
		$config = array(
               array(
                     'field'   => 'name',
                     'label'   => 'Name',
                     'rules'   => 'trim|required|xss_clean'
                  ),   
               array(
                     'field'   => 'email',
                     'label'   => 'Email',
                     'rules'   => 'trim|required|valid_email|callback__checkemail|xss_clean'
                  ),
			   array(
                     'field'   => 'phone',
                     'label'   => 'Phone',
                     'rules'   => 'trim|required|is_natural|xss_clean'
                  ),
			   array(
                     'field'   => 'address',
                     'label'   => 'Address',
                     'rules'   => 'trim|required|xss_clean'
                  )
            );

		$this->form_validation->set_rules($config); 
		if ($this->form_validation->run() == FALSE){
			return false;
		}
		else{
			//save to database..
			$this->model_user->edit_kar($id);
			return true;
		}
	}
	
	public function changepass(){
		$id =$this->uri->segment(4, 0);
		$data['detail'] = $this->model_user->getDetail($id);
		$data['title'] = TITLE.' - Rubah Password';
		if($this->input->post("btnSave") ){
			$hasil = $this->_changepassword($id);
			if($hasil) $data['success'] = "Kata Sandi Sudah di rubah";
		}
		$this->load->view("admin/karChpass",$data);
	}
	 
	
	function _changepassword($id){
		$this->form_validation->set_message('_checkpassword', 'Password lama salah');
		$this->form_validation->set_error_delimiters('', '');
		$config = array(
               array(
                     'field'   => 'password',
                     'label'   => 'Old Password',
                     'rules'   => 'trim|required|callback__checkpassword|xss_clean'
                  ),
               array(
                     'field'   => 'new_password',
                     'label'   => 'New Password',
                     'rules'   => 'trim|required|xss_clean'
                  ),
               array(
                     'field'   => 'new_password_c',
                     'label'   => 'Retype Password',
                     'rules'   => 'trim|required|matches[new_password]|xss_clean'
                  )
            );

		$this->form_validation->set_rules($config); 
		if ($this->form_validation->run() == FALSE){
			return false;
		}
		else{
			//save to database..
			$this->model_user->changepasswordKar($id,$this->input->post("new_password",true));
			return true;
		}
	}
}